package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.api.publics.MapApi;
import com.jzo2o.api.publics.dto.response.LocationResDTO;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.*;
import com.jzo2o.customer.mapper.AddressBookMapper;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 地址薄 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-07-06
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {
    @Resource
    private AddressBookMapper addressBookMapper;


    @Override
    public List<AddressBookResDTO> getByUserIdAndCity(Long userId, String city) {

        List<AddressBook> addressBooks = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getCity, city)
                .list();
        if(CollUtils.isEmpty(addressBooks)) {
            return new ArrayList<>();
        }
        return BeanUtils.copyToList(addressBooks, AddressBookResDTO.class);
    }

    /**
     * 新增地址簿
     *@param dto 地址簿的请求数据传输对象，包含需要添加的地址簿信息
     */
    @Override
    public void addAddressBook(AddressBookUpsertReqDTO dto) {

        AddressBook addressBook = BeanUtil.copyProperties(dto, AddressBook.class);
        addressBook.setUserId(UserContext.currentUserId());
        //如果添加的地址簿是默认地址，则将之前的默认地址设置为非默认地址
        if (dto.getIsDefault()==1){
            this.update(new UpdateWrapper<AddressBook>()
                    .set("is_default", 0)
                    .eq("is_default", 1)
                    .eq("user_id", UserContext.currentUserId()));
            addressBookMapper.insert(addressBook);
        }
        //如果添加的地址簿不是默认地址，则直接添加
        if (dto.getIsDefault()==0){
            addressBookMapper.insert(addressBook);
        }
    }

    /**
     * 分页查询地址簿
     * @param dto 地址簿分页参数
     * @return
     */
    @Override
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto,()-> baseMapper.getByUserId(UserContext.currentUserId()));
    }

    /**
     * 地址簿批量删除
     * @param ids 地址簿id集合
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        this.removeByIds(ids);
        //逻辑删除
        //this.update(new UpdateWrapper<AddressBook>().set("is_deleted", 1).in("id", ids));
    }

    /**
     * 设置默认地址
     * @param flag 是否是默认地址
     * @param id 地址簿id
     */
    @Override
    public void setDefault(Integer flag, Long id) {
        //如果要将该地址设置为默认地址，之前的默认地址需要设置为非默认地址
        if (flag == 1) {
            addressBookMapper.setDefault(flag, id);
            this.update(new UpdateWrapper<AddressBook>().set("is_default", 0).eq("is_default", 1).eq("user_id", UserContext.currentUserId()).ne("id", id));

        }

        if (flag == 0) {
            addressBookMapper.setDefault(flag, id);
        }
    }

    @Override
    public AddressBookResDTO getDefaultAddress() {
        AddressBook addressBook = addressBookMapper.getDefaultAddress();
        if (ObjectUtil.isNull(addressBook)) {
            return new AddressBookResDTO();
        }
        return BeanUtil.copyProperties(addressBook, AddressBookResDTO.class);
    }
}
